<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { statisticsApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading, allNumber } from '@/utils/public';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
  })
  interface formQuert {
    week:string
    gift1:number
    gift2:number
    gift3:number
    rate1:number
    rate2:number
    rate3:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    week: [{ required: true, trigger: "blur", message: "周不可为空" }],
    gift1: [{ required: true, trigger: "blur", message: "请填写礼物ID" }],
    gift2: [{ required: true, trigger: "blur", message: "请填写礼物ID" }],
    gift3: [{ required: true, trigger: "blur", message: "请填写礼物ID" }],
    rate1: [{ required: true, trigger: "blur", message: "请填写礼物一积分倍率" }],
    rate2: [{ required: true, trigger: "blur", message: "请填写礼物二积分倍率" }],
    rate3: [{ required: true, trigger: "blur", message: "请填写礼物三积分倍率" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      statisticsApi.getGiftstarConfigAdd(form).then(res=>{
        loading.close()
        ToastSuccess('新增成功')
        closeDialog()
        $emit('updateList')
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const openDialog = (query:any = {})=> {
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="50%"
    title="新增周星礼物">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="礼物周期" prop="week">
            <el-date-picker
              v-model="form.week"
              type="week"
              format="YYYY 第 ww 周"
              placeholder="选择周"
            />
          </el-form-item>
        <div class="flex">
          <el-form-item :label-width="formLabelWidth" label="礼物一" prop="gift1">
            <el-input
              v-model="form.gift1"
              @input="allNumber(form,'gift1')"
              placeholder="请填写礼物ID" />
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="积分倍率" prop="rate1">
            <el-input
              v-model="form.rate1"
              @input="allNumber(form,'rate1')"
              placeholder="请填写礼物一积分倍率" />
          </el-form-item>
        </div>
        <div class="flex">
          <el-form-item :label-width="formLabelWidth" label="礼物二" prop="gift2">
            <el-input
              v-model="form.gift2"
              @input="allNumber(form,'gift2')"
              placeholder="请填写礼物ID" />
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="积分倍率" prop="rate2">
            <el-input
              v-model="form.rate2"
              @input="allNumber(form,'rate2')"
              placeholder="请填写礼物二积分倍率" />
          </el-form-item>
        </div>
        <div class="flex">
          <el-form-item :label-width="formLabelWidth" label="礼物三" prop="gift3">
            <el-input
              v-model="form.gift3"
              @input="allNumber(form,'gift3')"
              placeholder="请填写礼物ID" />
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="积分倍率" prop="rate3">
            <el-input
              v-model="form.rate3"
              @input="allNumber(form,'rate3')"
              placeholder="请填写礼物三积分倍率" />
          </el-form-item>
        </div>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>